<template>
  <div class="list-page">
    <div class="list-page-main">
      <NDataTable
        style="flex: 1"
        flex-height
        :columns="columns"
        :data="tableData"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import { h, ref } from 'vue'
import { NButton, NDataTable, useMessage, MessageType } from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import { fulituDown, fulituRu, jkDown, jkRu } from '@/views/python/list/model'
const message = useMessage()
const types: MessageType[] = ['success', 'info', 'warning', 'error', 'loading']
const handDown = (row: any) => {
  if (row.institution === 'fulitu') {
    let notice = message.create(
      `初始化下载${row.institution}-${row.classify}`,
      {
        type: types[4],
        duration: 0
      }
    )
    fulituDown(row, {
      back: backInfo => {
        notice.content = `下载${row.institution}-${row.classify}: 成功: ${
          backInfo.success
        } 失败:${backInfo.error} 共处理:${backInfo.success + backInfo.error}/${
          backInfo.total
        }`
      },
      success: () => {
        notice.type = types[0]
        notice.content = '下载完成'
        setTimeout(() => {
          notice.destroy()
          notice = null
        }, 3000)
      }
    })
  } else if (row.institution === 'jk.ts') {
    let notice = message.create(
      `初始化下载${row.institution}-${row.classify}`,
      {
        type: types[4],
        duration: 0
      }
    )
    jkDown(row, {
      back: backInfo => {
        notice.content = `下载${row.institution}-${row.classify}: 成功: ${
          backInfo.success
        } 失败:${backInfo.error} 共处理:${backInfo.success + backInfo.error}/${
          backInfo.total
        }`
      },
      success: () => {
        notice.type = types[0]
        notice.content = '下载完成'
        setTimeout(() => {
          notice.destroy()
          notice = null
        }, 3000)
      }
    })
  }
}
const handJoin = (row: any) => {
  if (row.institution === 'fulitu') {
    let notice = message.create(
      `初始化入库${row.institution}-${row.classify}`,
      {
        type: types[4],
        duration: 0
      }
    )
    fulituRu(row, {
      back: backInfo => {
        notice.content = `入库${row.institution}-${row.classify}: 成功: ${
          backInfo.success
        } 失败:${backInfo.error} 共处理:${backInfo.success + backInfo.error}/${
          backInfo.total
        }`
      },
      success: () => {
        notice.type = types[0]
        notice.content = '入库完成'
        setTimeout(() => {
          notice.destroy()
          notice = null
        }, 3000)
      }
    })
  } else if (row.institution === 'jk.ts') {
    let notice = message.create(
      `初始化入库${row.institution}-${row.classify}`,
      {
        type: types[4],
        duration: 0
      }
    )
    jkRu(row, {
      back: backInfo => {
        notice.content = `入库${row.institution}-${row.classify}: 成功: ${
          backInfo.success
        } 失败:${backInfo.error} 共处理:${backInfo.success + backInfo.error}/${
          backInfo.total
        }`
      },
      success: () => {
        notice.type = types[0]
        notice.content = '入库完成'
        setTimeout(() => {
          notice.destroy()
          notice = null
        }, 3000)
      }
    })
  }
}
const columns: DataTableColumns<any> = [
  {
    title: '机构',
    key: 'institution'
  },
  {
    title: '分类',
    key: 'classify'
  },
  {
    title: '数据',
    key: 'total'
  },
  {
    title: '操作',
    key: 'total',
    align: 'center',
    width: 300,
    fixed: 'right',
    render(row: any) {
      return h('div', [
        h(
          NButton,
          {
            size: 'small',
            type: 'success',
            onClick: () => handDown(row)
          },
          { default: () => '下载' }
        ),
        h(
          NButton,
          {
            size: 'small',
            type: 'success',
            style: {
              marginLeft: '10px'
            },
            onClick: () => handJoin(row)
          },
          { default: () => '入库' }
        )
      ])
    }
  }
]
const tableData = ref([
  {
    institution: 'fulitu',
    classify: '喵写真enen ',
    total: 41,
    queryForm: {
      name: 'MiaoXieZhen',
      type_id: 18,
      institution_id: 13
    }
  },
  {
    institution: 'fulitu',
    classify: '紧急企划',
    total: 51,
    queryForm: {
      name: 'JinJiQiHua',
      type_id: 18,
      institution_id: 14
    }
  },
  {
    institution: 'fulitu',
    classify: '少女秩序',
    total: 45,
    queryForm: {
      name: 'ShaoNvZhiXu',
      type_id: 18,
      institution_id: 2
    }
  },
  {
    institution: 'fulitu',
    classify: '稚乖画册',
    total: 15,
    queryForm: {
      name: 'ZhiGuaiHuaCe',
      type_id: 18,
      institution_id: 9
    }
  },
  {
    institution: 'fulitu',
    classify: '轻兰映画',
    total: 54,
    queryForm: {
      name: 'QingLanYingHua',
      type_id: 18,
      institution_id: 16
    }
  },
  {
    institution: 'fulitu',
    classify: '风之领域',
    total: 192,
    queryForm: {
      name: 'FengZhiLingYu',
      type_id: 17,
      institution_id: 17
    }
  },
  {
    institution: 'fulitu',
    classify: '森萝财团',
    total: 332,
    queryForm: {
      name: 'SenLuoCaiTuan',
      type_id: 18,
      institution_id: 1
    }
  },
  {
    institution: 'fulitu',
    classify: '喵糖映画',
    total: 467,
    queryForm: {
      name: 'MiaoTangYingHua',
      type_id: 18,
      institution_id: 18
    }
  },
  {
    institution: 'fulitu',
    classify: '微博网红',
    total: 2802,
    queryForm: {
      name: 'wanghong',
      type_id: 18,
      institution_id: 5
    }
  },
  {
    institution: 'jk.ts',
    classify: 'JK',
    total: 14,
    queryForm: {
      name: 'JK',
      type_id: 19,
      institution_id: 4
    }
  },
  {
    institution: 'jk.ts',
    classify: '日系',
    total: 6,
    queryForm: {
      name: 'sunshine',
      type_id: 19,
      institution_id: 4
    }
  },
  {
    institution: 'jk.ts',
    classify: '小清新',
    total: 5,
    queryForm: {
      name: 'comely',
      type_id: 19,
      institution_id: 4
    }
  },
  {
    institution: 'jk.ts',
    classify: '情绪',
    total: 2,
    queryForm: {
      name: 'emotion',
      type_id: 19,
      institution_id: 4
    }
  }
])
</script>
<style scoped lang="scss"></style>
